package reader;

import java.io.*;
import java.util.*;

public class ReaderFromFile {

	private BufferedReader in = null;
	private Map<Integer, Map<String, String>> map = null;
	private String[] valuesOfTheFirstLine = null;

	public ReaderFromFile(File file) {
		try {
			in = new BufferedReader(new FileReader(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public Map<Integer, Map<String, String>> getMap() {
		String line = null;
		int count = 0;

		map = new HashMap<Integer, Map<String, String>>();

		try {
			valuesOfTheFirstLine = splitLine(in.readLine());

			while ((line = in.readLine()) != null) {
				map.put(count, addValuesToList(splitLine(line)));
				count++;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return map;
	}

	private Map<String, String> addValuesToList(String[] slpitedLine) {
		Map<String, String> list = new HashMap<String, String>();
		for (int i = 0; i < slpitedLine.length; i++)
			list.put(valuesOfTheFirstLine[i], slpitedLine[i]);

		return list;
	}

	private String[] splitLine(String line) {
		return line.split(",");
	}
}
